package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.sun.org.apache.bcel.internal.generic.D2F;
import com.ymatou.iapi.sellercs.parameter.GetMessageListBean;
import com.ymatou.iapi.sellercs.parameter.MessageResp;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageWithAttachBean;
import com.ymatou.iapi.sellercs.service.GetMessageListCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtCssession;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*获取私信列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMessageList{
	private static GetMessageListBean getmessagelistBean;
	private static GetMessageListCall getmessagelistCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取私信列表");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		getmessagelistBean=new GetMessageListBean();
		getmessagelistCall=new GetMessageListCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmessagelistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyCount(SendMessageBean sendmessageBean, int i) {
		// TODO 计数服务验证
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageList_001() {
		Logger.start(true,"获取私信列表成功,IsNew=true,ClearUnRead=true,LongPostTime=0");
		try {
			//IsNew=true获取时间线之后的数据 
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			int unread=SellerCSIMCallService.getUnReadNum(sellerId);
			
			getmessagelistBean.setClearUnRead(true);
			getmessagelistBean.setCSId(sellerId);
			getmessagelistBean.setIsNew(false);
			getmessagelistBean.setLongTimeline(0l);
			getmessagelistBean.setMasterCSId(sellerId);
			getmessagelistBean.setMessageCount(20);
			getmessagelistBean.setTimeline(YMTDateUtil.getBeforeOrNextDate(-1));
			getmessagelistBean.setToUserId(userId);
			getmessagelistCall.setData(getmessagelistBean);
			getmessagelistCall.callService();			
			Logger.debug("getmessagelistCall:"+FormatUtil.beanToGSONString(getmessagelistCall.getResult()));
			Logger.verifyEquals("200", getmessagelistCall.getString("Status"), "验证Status");
			ArrayList<MessageResp> list = getmessagelistCall.getResult();
			//[{"MessageId":"24716664_24635724_200809","PostTime":"2018-10-15T11:30:57","LongPostTime":1539574257950,"MsgFromId":24716664,"MsgToId":24635724,"Message":"connect","MsgFrom":"autotestP26i64z","MsgTo":"autotest1","SessionNumber":"20181015113057_24635724_24716664","MessageAttachList":[]}]

			Logger.verifyEquals(1, list.size(), "验证记录数");
			Logger.verifyNotNull(list.get(0).getMessageId(), "验证msgid");
			Logger.verifyEquals(getmessagelistBean.getCSId(),list.get(0).getCsId(), "验证csid");
			Logger.verifyEquals(JUserCallService.GetUserInfo(getmessagelistBean.getCSId()).getLoginId(),list.get(0).getCsName(), "验证CSName");
			Logger.verifyEquals(sendmessageBean.getFromUserId(),list.get(0).getMsgFromId(), "验证msgfromid");
			Logger.verifyEquals(sendmessageBean.getToUserId(),list.get(0).getMsgToId(), "验证msgtoid");
			Logger.verifyEquals(sendmessageBean.getMessage(),list.get(0).getMessage(), "验证Message");
			
			Logger.verifyEquals(JUserCallService.GetUserInfo(sendmessageBean.getFromUserId()).getLoginId(),list.get(0).getMsgFrom(), "验证msgfrom");
			Logger.verifyEquals(JUserCallService.GetUserInfo(sendmessageBean.getToUserId()).getLoginId(),list.get(0).getMsgTo(), "验证msgto");
			
			Logger.verifyEquals(true ,unread-SellerCSIMCallService.getUnReadNum(sellerId)==1,"验证未读数变化一条");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetMessageList_002() {
		Logger.start(true,"不输入csid和masterccsid,无数据");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			getmessagelistBean.setClearUnRead(true);
//			getmessagelistBean.setCSId(sellerId);
			getmessagelistBean.setIsNew(false);
			getmessagelistBean.setLongTimeline(0l);
//			getmessagelistBean.setMasterCSId(sellerId);
			getmessagelistBean.setMessageCount(20);
			getmessagelistBean.setTimeline(YMTDateUtil.getBeforeOrNextDate(-1));
			getmessagelistBean.setToUserId(userId);
			getmessagelistCall.setData(getmessagelistBean);
			getmessagelistCall.callService();			
			Logger.verifyEquals("200", getmessagelistCall.getString("Status"), "验证Status");
			Logger.verifyEquals(0,getmessagelistCall.getJsonObject().get("Result").getAsJsonArray().size(),"验证没有数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetMessageList_003() {
		Logger.start(true,"不输入ToUserId失败");
		try {
			getmessagelistBean.setClearUnRead(true);
			getmessagelistBean.setCSId(sellerId);
			getmessagelistBean.setIsNew(false);
			getmessagelistBean.setLongTimeline(0l);
			getmessagelistBean.setMasterCSId(sellerId);
			getmessagelistBean.setMessageCount(20);
			getmessagelistBean.setTimeline(YMTDateUtil.getBeforeOrNextDate(-1));
//			getmessagelistBean.setToUserId(userId);
			getmessagelistCall.setData(getmessagelistBean);
			getmessagelistCall.callService();			
			Logger.verifyEquals("200", getmessagelistCall.getString("Status"), "验证Status");
			Logger.verifyEquals(0,getmessagelistCall.getJsonObject().get("Result").getAsJsonArray().size(),"验证没有数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetMessageList_004() {
		Logger.start(true,"获取私信列表成功,IsNew=false不获取最新消息");
		//设置时间线在当前时间之前
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			//期望获得一天以前的数据？
			getmessagelistBean.setClearUnRead(true);
			getmessagelistBean.setCSId(sellerId);
			getmessagelistBean.setIsNew(false);
			getmessagelistBean.setLongTimeline(YMTDateUtil.getBeforeOrNextDate(-1).getTime());
			getmessagelistBean.setMasterCSId(sellerId);
			getmessagelistBean.setMessageCount(20);
			getmessagelistBean.setToUserId(userId);
			getmessagelistCall.setData(getmessagelistBean);
			//调用2次
			getmessagelistCall.callService();			
			Logger.verifyEquals("200", getmessagelistCall.getString("Status"), "验证Status");
			Logger.verifyEquals(0,getmessagelistCall.getJsonObject().get("Result").getAsJsonArray().size(),"验证没有数据");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//im获取强制清理未读数 字段无效
	public void Tc_GetMessageList_005() {
		Logger.start(true,"获取私信列表成功,ClearUnRead=false不清除未读数");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			int unread=SellerCSIMCallService.getUnReadNum(sellerId);
			
			getmessagelistBean.setClearUnRead(false);
			getmessagelistBean.setCSId(sellerId);
			getmessagelistBean.setIsNew(false);
			getmessagelistBean.setLongTimeline(0l);
			getmessagelistBean.setMasterCSId(sellerId);
			getmessagelistBean.setMessageCount(20);
			getmessagelistBean.setTimeline(YMTDateUtil.getBeforeOrNextDate(-1));
			getmessagelistBean.setToUserId(userId);
			getmessagelistCall.setData(getmessagelistBean);
			getmessagelistCall.callService();			
			Logger.verifyEquals("200", getmessagelistCall.getString("Status"), "验证Status");
						int unread2=SellerCSIMCallService.getUnReadNum(sellerId);
			Logger.verifyEquals(unread ,unread2,"验证未读数变化一条");
			
			ArrayList<MessageResp> list = getmessagelistCall.getResult();
			//[{"MessageId":"24716664_24635724_200809","PostTime":"2018-10-15T11:30:57","LongPostTime":1539574257950,"MsgFromId":24716664,"MsgToId":24635724,"Message":"connect","MsgFrom":"autotestP26i64z","MsgTo":"autotest1","SessionNumber":"20181015113057_24635724_24716664","MessageAttachList":[]}]

			Logger.verifyEquals(1, list.size(), "验证记录数");
			Logger.verifyNotNull(list.get(0).getMessageId(), "验证msgid");
			Logger.verifyEquals(getmessagelistBean.getCSId(),list.get(0).getCsId(), "验证csid");
			Logger.verifyEquals(JUserCallService.GetUserInfo(getmessagelistBean.getCSId()).getLoginId(),list.get(0).getCsName(), "验证CSName");
			Logger.verifyEquals(sendmessageBean.getFromUserId(),list.get(0).getMsgFromId(), "验证msgfromid");
			Logger.verifyEquals(sendmessageBean.getToUserId(),list.get(0).getMsgToId(), "验证msgtoid");
			Logger.verifyEquals(sendmessageBean.getMessage(),list.get(0).getMessage(), "验证Message");
			
			Logger.verifyEquals(JUserCallService.GetUserInfo(sendmessageBean.getFromUserId()).getLoginId(),list.get(0).getMsgFrom(), "验证msgfrom");
			Logger.verifyEquals(JUserCallService.GetUserInfo(sendmessageBean.getToUserId()).getLoginId(),list.get(0).getMsgTo(), "验证msgto");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetMessageList_006() {
		Logger.start(true,"获取带附件私信列表成功");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageWithAttachBean smwa = SellerCSIMCallService.SendMessageWithAttach(userId, sellerId);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			
			getmessagelistBean.setClearUnRead(true);
			getmessagelistBean.setCSId(sellerId);
			getmessagelistBean.setIsNew(false);
			getmessagelistBean.setLongTimeline(0l);
			getmessagelistBean.setMasterCSId(sellerId);
			getmessagelistBean.setMessageCount(20);
			getmessagelistBean.setToUserId(userId);
			getmessagelistCall.setData(getmessagelistBean);
			getmessagelistCall.callService();			
			Logger.debug("getmessagelistCall:"+FormatUtil.beanToGSONString(getmessagelistCall.getResult()));
			Logger.verifyEquals("200", getmessagelistCall.getString("Status"), "验证Status");
			ArrayList<MessageResp> list = getmessagelistCall.getResult();
			//[{"MessageId":"24716664_24635724_200809","PostTime":"2018-10-15T11:30:57","LongPostTime":1539574257950,"MsgFromId":24716664,"MsgToId":24635724,"Message":"connect","MsgFrom":"autotestP26i64z","MsgTo":"autotest1","SessionNumber":"20181015113057_24635724_24716664","MessageAttachList":[]}]

			Logger.verifyEquals(1, list.size(), "验证记录数");
			Logger.verifyNotNull(list.get(0).getMessageId(), "验证msgid");
			Logger.verifyEquals(getmessagelistBean.getCSId(),list.get(0).getCsId(), "验证csid");
			Logger.verifyEquals(JUserCallService.GetUserInfo(getmessagelistBean.getCSId()).getLoginId(),list.get(0).getCsName(), "验证CSName");
			Logger.verifyEquals(smwa.getMessageAttachList().size(),list.get(0).getMessageAttachList().size(),"验证附件列表");
			Logger.debug("send:"+FormatUtil.beanToGSONString(smwa.getMessageAttachList()));
			Logger.debug("rt"+FormatUtil.beanToGSONString(list.get(0).getMessageAttachList()));
			List<String> contentlist = smwa.getMessageAttachList().stream().map(map->{return map.getContent();}).collect(Collectors.toList());
			List<Integer> typelist = smwa.getMessageAttachList().stream().map(map->{return map.getType();}).collect(Collectors.toList());
			list.get(0).getMessageAttachList().forEach(ma->{
				Logger.verifyEquals(true, typelist.contains(ma.getType()), "验证type");
				Logger.verifyEquals(true, contentlist.contains(ma.getContent()), "验证content");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMessageList_007() {
		Logger.start(true,"获取私信列表成功,商家发送的导流信息带上tip");
		try {
			//IsNew=true获取时间线之后的数据 
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
//			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SellerSendMessage(sellerId, userId, "我是买手加微信:135-1234-1234");
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			int unread=SellerCSIMCallService.getUnReadNum(sellerId);
			
			getmessagelistBean.setClearUnRead(true);
			getmessagelistBean.setCSId(sellerId);
			getmessagelistBean.setIsNew(false);
			getmessagelistBean.setLongTimeline(0l);
			getmessagelistBean.setMasterCSId(sellerId);
			getmessagelistBean.setMessageCount(20);
			getmessagelistBean.setTimeline(YMTDateUtil.getBeforeOrNextDate(-1));
			getmessagelistBean.setToUserId(userId);
			getmessagelistCall.setData(getmessagelistBean);
			getmessagelistCall.callService();			
			Logger.debug("getmessagelistCall:"+FormatUtil.beanToGSONString(getmessagelistCall.getResult()));
			Logger.verifyEquals("200", getmessagelistCall.getString("Status"), "验证Status");
			ArrayList<MessageResp> list = getmessagelistCall.getResult();
			//[{"MessageId":"24716664_24635724_200809","PostTime":"2018-10-15T11:30:57","LongPostTime":1539574257950,"MsgFromId":24716664,"MsgToId":24635724,"Message":"connect","MsgFrom":"autotestP26i64z","MsgTo":"autotest1","SessionNumber":"20181015113057_24635724_24716664","MessageAttachList":[]}]

			Logger.verifyEquals(1, list.size(), "验证记录数");
			Logger.verifyNotNull(list.get(0).getMessageId(), "验证msgid");
			Logger.verifyEquals(getmessagelistBean.getCSId(),list.get(0).getCsId(), "验证csid");
			Logger.verifyEquals(JUserCallService.GetUserInfo(getmessagelistBean.getCSId()).getLoginId(),list.get(0).getCsName(), "验证CSName");
			
			Logger.verifyEquals(2,list.get(0).getMessageAttachList().get(0).getType(),"验证type");
			Logger.verifyIsContains("屏蔽",list.get(0).getMessageAttachList().get(0).getContent(),"验证Content");
			Logger.verifyEquals(true,list.get(0).getIsCancel(),"验证IsCancel");
			Logger.verifyEquals(true ,unread-SellerCSIMCallService.getUnReadNum(sellerId)==0,"验证未读数无变化");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}